package com.beyond.trace.controller.storage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beyond.trace.domain.storage.Decrease;
import com.beyond.trace.domain.storage.Increase;
import com.beyond.trace.domain.storage.Produce;
import com.beyond.trace.requestVo.ProduceQuery;
import com.beyond.trace.service.storage.IProduceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.SnowUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 加工记录
 * 
 * @author yao
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/produce")
@Api(value = "加工记录", tags = "加工记录")
public class ProduceController extends BaseController
{
    @Autowired
    private IProduceService produceService;

    @GetMapping("/getProducePage")
    @ApiOperation("分页查询加工记录")
    public TableDataInfo getProducePage(ProduceQuery productQuery)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            productQuery.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        }
        List<Produce> list = produceService.getProducePage(productQuery);
        return getDataTable(list);
    }

    @GetMapping(value = "/getProduceById")
    @ApiOperation("通过id查询加工记录")
    public AjaxResult getProduceById(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        if(produceService.getById(id) == null) {
            return AjaxResult.error("没有查到信息");
        }
        QueryWrapper<Produce> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return AjaxResult.success(produceService.getOne(queryWrapper));
    }

    @PostMapping(value = "/addProduce")
    @ApiOperation("添加加工记录")
    public AjaxResult addProduce(@RequestBody @Validated Produce produce)
    {
        produce.setCreateBy(SecurityUtils.getUsername());
        produce.setUpdateBy(SecurityUtils.getUsername());
        produce.setCreateTime(new Date());
        produce.setUpdateTime(new Date());
        if(StringUtils.isEmpty(produce.getProduceNo())) {
            produce.setProduceNo(SnowUtil.getId());
        }
        produce.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        return AjaxResult.success(produceService.save(produce));
    }

    @PostMapping(value = "/editProduce")
    @ApiOperation("修改加工记录")
    public AjaxResult editProduce(@RequestBody Produce produce)
    {
        if(StringUtils.isEmpty(produce.getId())) {
            return AjaxResult.error("id不能为空");
        }
        produce.setUpdateBy(SecurityUtils.getUsername());
        produce.setUpdateTime(new Date());
        return AjaxResult.success(produceService.updateById(produce));
    }

    @GetMapping(value = "/deleteProduceById")
    @ApiOperation("删除加工记录")
    public AjaxResult deleteProduceById(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        List<String> list = Arrays.asList(id.split(","));
        QueryWrapper<Produce> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",list);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return AjaxResult.success(produceService.remove(queryWrapper));
    }



}
